package com.cyt.work.antiepidemic.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.antiepidemic.dao.AntiepidemicRecordDao;
import com.cyt.work.antiepidemic.model.po.AntiepidemicRecord;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicRecordExportVo;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicRecordVo;
import com.cyt.work.antiepidemic.service.AntiepidemicRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * e门通-社区出入信息Service层实现类
 *
 * @author rong yuan
 * @date 2020-2-12 2:24:25
 */
@Service("antiepidemicRecordService")
public class AntiepidemicRecordServiceImpl extends BaseServiceImpl<AntiepidemicRecord> implements AntiepidemicRecordService {

    @Resource
    private AntiepidemicRecordDao antiepidemicRecordDao;

    @Autowired
    public void setBaseDao(AntiepidemicRecordDao antiepidemicRecordDao) {
        this.baseDao = antiepidemicRecordDao;
    }

    /**
     * 分页查询
     *
     * @param antiepidemicRecordVo 接受参数对象
     * @param pageNum              分页数
     * @param pageSize             每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:25
     */
    @Override
    public ReturnResult page(AntiepidemicRecordVo antiepidemicRecordVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<AntiepidemicRecordVo> list = antiepidemicRecordDao.selectByCondition(antiepidemicRecordVo);
        PageInfo<AntiepidemicRecordVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 根据ID获取数据
     *
     * @param id 主键ID
     * @return AntiepidemicRecordVo
     * @author rong yuan
     * @date 2020-2-12 2:24:25
     */
    @Override
    public AntiepidemicRecordVo selectById(String id) {
        AntiepidemicRecordVo antiepidemicRecordVo = new AntiepidemicRecordVo();
        antiepidemicRecordVo.setId(id);
        List<AntiepidemicRecordVo> antiepidemicRecordVoList = antiepidemicRecordDao.selectByCondition(antiepidemicRecordVo);
        if (antiepidemicRecordVoList != null && antiepidemicRecordVoList.size() == 1) {
            antiepidemicRecordVo = antiepidemicRecordVoList.get(0);
        }
        return antiepidemicRecordVo;
    }

    /**
     * 添加或更新
     *
     * @param antiepidemicRecordVo 参数接受对象
     * @param sysUser              当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(AntiepidemicRecordVo antiepidemicRecordVo, SysUser sysUser) {
        if (StringUtils.isBlank(antiepidemicRecordVo.getId())) {
            AntiepidemicRecord antiepidemicRecord = new AntiepidemicRecord(sysUser);
            antiepidemicRecord.setApplyId(antiepidemicRecordVo.getApplyId());
            antiepidemicRecord.setPersonId(antiepidemicRecordVo.getPersonId());
            antiepidemicRecord.setAccessType(antiepidemicRecordVo.getAccessType());
            antiepidemicRecord.setBeginTime(DateUtil.parseDateTime(antiepidemicRecordVo.getBeginTime()));
            antiepidemicRecord.setAccessStatus(antiepidemicRecordVo.getAccessStatus());
            antiepidemicRecordDao.insert(antiepidemicRecord);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            AntiepidemicRecord antiepidemicRecord = antiepidemicRecordDao.selectByPrimaryKey(antiepidemicRecordVo.getId());
            antiepidemicRecord.setApplyId(antiepidemicRecordVo.getApplyId());
            antiepidemicRecord.setPersonId(antiepidemicRecordVo.getPersonId());
            antiepidemicRecord.setAccessType(antiepidemicRecordVo.getAccessType());
            antiepidemicRecord.setEndTime(DateUtil.parseDateTime(antiepidemicRecordVo.getEndTime()));
            antiepidemicRecord.setAccessStatus(antiepidemicRecordVo.getAccessStatus());
            antiepidemicRecord.setUpdateTime(new Date());
            antiepidemicRecord.setUpdateUser(sysUser.getId());
            antiepidemicRecordDao.updateByPrimaryKey(antiepidemicRecord);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param antiepidemicRecordVo 参数接受对象
     * @param sysUser              当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(AntiepidemicRecordVo antiepidemicRecordVo, SysUser sysUser) {
        //要修改的字段
        AntiepidemicRecord antiepidemicRecord = new AntiepidemicRecord();
        antiepidemicRecord.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        antiepidemicRecord.setUpdateUser(sysUser.getId());
        antiepidemicRecord.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(AntiepidemicRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicRecordVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicRecordVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        antiepidemicRecordDao.updateByExampleSelective(antiepidemicRecord, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param antiepidemicRecordVo 参数接受对象
     * @param sysUser              当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:24:25
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(AntiepidemicRecordVo antiepidemicRecordVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(AntiepidemicRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicRecordVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicRecordVo.getId().split(",")));
        }
        antiepidemicRecordDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public List<AntiepidemicRecordExportVo> selectExportList(AntiepidemicRecordVo antiepidemicRecordVo) {
        return antiepidemicRecordDao.selectExportList(antiepidemicRecordVo);
    }
}